package com.lhy.tool.generator;

import com.lhy.tool.autoconfigation.GeneratorProperties;
import com.lhy.tool.autoconfigation.GeneratorProperties.ModelAttributes.JavaAttributes;
import com.lhy.tool.model.Clazz;
import com.lhy.tool.model.CrudBean;
import com.lhy.tool.model.CrudColumn;
import com.lhy.tool.model.JavaAttribute;
import com.lhy.tool.util.StringUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.File;
import java.io.FileNotFoundException;

@Getter
@Setter
public abstract class AbstractJavaGenerator extends AbstractGenerator {

    JavaAttribute javaAttribute;

    @Override
    protected void writeAttribute(CrudBean crudBean, GeneratorProperties generatorProperties) {
        this.javaAttribute = javaAttribute(crudBean, generatorProperties);
        this.writeJavaAttribute(crudBean, generatorProperties);
    }

    protected abstract void writeJavaAttribute(CrudBean crudBean, GeneratorProperties generatorProperties);

    private JavaAttribute javaAttribute(CrudBean crudBean, GeneratorProperties generatorProperties) {
        JavaAttributes javaAttributes = crudBean.getModelAttributes().getJavaAttributes();
        String modelSimpleName = crudBean.getSimpleName();
        String primaryKeyType = crudBean.getColumns().stream().filter(CrudColumn::getPrimary).findFirst().orElse(new CrudColumn()).getColumnType();

        String packageName = generatorProperties.getGlobalConfig().getPackageName();
        String moduleName = crudBean.getModelAttributes().getModuleName();
        return JavaAttribute.builder().description(crudBean.getDescription())
                .model(getClazzFile(packageName, moduleName, javaAttributes.getModelPackage(), modelSimpleName, "%s"))
                .controller(getClazzFile(packageName, moduleName, javaAttributes.getControllerPackage(), modelSimpleName, generatorProperties.getGlobalConfig().getControllerFomatter()))
                .service(getClazzFile(packageName, moduleName, javaAttributes.getServicePackage(), modelSimpleName, generatorProperties.getGlobalConfig().getServiceFomatter()))
                .mapperXml(getClazzFile(packageName, moduleName, javaAttributes.getMapperXmlPackage(), modelSimpleName, generatorProperties.getGlobalConfig().getMapperXmlFomatter()))
                .mapper(getClazzFile(packageName, moduleName, javaAttributes.getMapperPackage(), modelSimpleName, generatorProperties.getGlobalConfig().getMapperFomatter()))
                .primaryKeyType(primaryKeyType)
                .build();
    }

    public Clazz getClazzFile(String packageName, String moduleName, String childPackageName, String moduleSimpleName, String fileName) {
        String realName = String.format(fileName, moduleSimpleName);
        String format = String.format("%s.%s.%s.%s", packageName, moduleName, childPackageName, realName);
        return new Clazz(format);
    }

    /**
     * @return
     * @Title: curClzz
     * @Description: 当前需要生成的类
     */
    protected abstract Clazz curClazz(JavaAttribute javaAttribute);

    protected String queryOrderSql(CrudBean crudBean) {
        return crudBean.getModelAttributes().getJavaAttributes().getQueryOrderSql();
    }

    /**
     * 生成的代码文件
     */
    @Override
    protected File getCodeFile() {
        Clazz curClazz = this.curClazz(this.javaAttribute);
        String packageName = curClazz.getPackageName();
        String filePath = this.fileAttribute.getProjectPath() + "src/main/java/" + StringUtil.replacePointToLinuxSeparator(packageName)
                + "/" + curClazz.getName() + ".java";
        return new File(filePath);
    }

}
